package com.hbmcc.heshenghuo.flow

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.app.Activity
import android.util.Log
import android.view.View
import com.hbmcc.heshenghuo.databinding.FloorMyFrgFlowBinding
import com.hbmcc.heshenghuo.databinding.FloorMyFrgPagerBinding
import com.hbmcc.heshenghuo.model.http.bean.TitleAndUrlBean
import com.hbmcc.heshenghuo.pager.FloorMyFrgPagerAdapter
import com.hbmcc.heshenghuo.util.Formatter.ssDateFormatter
import com.hbmcc.heshenghuo.view.FloorTitle
import java.util.*

/**
 * @author 支豪亮
 * @date 2021/11/19 9:11 上午
 * @des 流量楼层
 */
class FloorMyFrgFlowView(val view: View) : FlowViewItemListener {

    private val dataBing = FloorMyFrgFlowBinding.bind(view)

    private lateinit var data: FloorMyFrgFlow

    private var timer = Timer()

    init {
        initTitle()
    }

    private fun initTitle() {
        val titleAndUrlBean = TitleAndUrlBean()
        titleAndUrlBean.titleType = 2
        titleAndUrlBean.isTitle = "1"
        titleAndUrlBean.titleIconUrl = "https://t7.baidu.com/it/u=2605426091,1199286953&fm=193&f=GIF"
        titleAndUrlBean.fuTitle = "全国流量套餐78元(2019存量)"
        titleAndUrlBean.moreUrl = "https://www.baidu.com/"
        titleAndUrlBean.moreName = "查看详情"

        setFloorTitle(dataBing.floorTitle, titleAndUrlBean, "123344")

        initData()
    }

    private fun initData() {
        val item1 = FloorMyFrgTopItem("通用流量", "切换定向流量", "流量总量", "2180MB", "剩余流量", "1024MB", "0.5")
        val item2 = FloorMyFrgTopItem("定向流量", "切换通用流量", "流量总量", "1024MB", "剩余流量", "716MB", "0.3")
        val item3 = FloorMyFrgTopItem("语音", "点击切换短信", "语音总量", "120分钟", "剩余语音", "36分钟", "0.7")
        val item4 = FloorMyFrgTopItem("短信", "点击切换语音", "短信总量", "100条", "剩余短信", "40条", "0.6")

        data = FloorMyFrgFlow(listOf(item1, item2), listOf(item3, item4))
        dataBing.viewModel = data
        dataBing.callback = this

        startTimer()
    }

    fun startTimer() {
        if ((data.topLeftList.isNullOrEmpty() && data.topRightList.isNullOrEmpty()) || (data.topLeftList!!.size < 2 && data.topRightList!!.size < 2) || (data.isLeftStop && data.isRightStop)) return

        timer.cancel()
        timer = Timer()
        timer.schedule(TimerTask(), 5000, 5000)
    }

    fun stopTimer() {
        timer.cancel()
    }

    private fun flipit(isFrist: Boolean) {
        if (isFrist && (data.topLeftList.isNullOrEmpty() || data.topLeftList!!.size < 2)) return

        if (!isFrist && (data.topRightList.isNullOrEmpty() || data.topRightList!!.size < 2)) return

        val view: View = if (isFrist) dataBing.incTopLeft.root else dataBing.incTopRight.root
        val visToInvis = ObjectAnimator.ofFloat(view, "rotationY", 0f, 90f)
        visToInvis.duration = 500
        visToInvis.interpolator = FloorMyFrgPagerAdapter.accelerator
        val invisToVis = ObjectAnimator.ofFloat(view, "rotationY", -90f, 0f)
        invisToVis.duration = 500
        invisToVis.interpolator = FloorMyFrgPagerAdapter.decelerator
        visToInvis.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(anim: Animator) {
                invisToVis.start()
                if (isFrist) {
                    data.leftIndex = initIndex(data.leftIndex)
                    dataBing.incTopLeft.viewModel = data.current(true)
                    dataBing.incTopLeft.executePendingBindings()
                } else {
                    data.rightIndex = initIndex(data.rightIndex)
                    dataBing.incTopRight.viewModel = data.current(false)
                    dataBing.incTopRight.executePendingBindings()
                }
            }
        })
        visToInvis.start()
    }

    private fun initIndex(index: Int) = if (index == 0) 1 else 0

    /***
     * 原来项目就有的功能
     */
    private fun setFloorTitle(floorTitle: FloorTitle?, titleAndUrlBean: TitleAndUrlBean?, type: String?) {
        if (floorTitle == null) return
        floorTitle.setBean(titleAndUrlBean)
        floorTitle.channel = type
    }

    private inner class TimerTask : java.util.TimerTask() {
        override fun run() {
            (view.context as Activity).runOnUiThread() {
                Log.e("TimerTask", "${ssDateFormatter.format(Date())}")
                if (!data.isLeftStop) flipit(true)
                if (!data.isRightStop) flipit(false)
                if ((data.topLeftList.isNullOrEmpty() || data.topLeftList!!.size < 2 || data.isLeftStop) && (data.topRightList.isNullOrEmpty() || data.topRightList!!.size < 2 || data.isRightStop)) {
                    timer.cancel()
                }
            }

        }
    }

    override fun onTopClickItem(isFrist: Boolean) {
        flipit(isFrist)
        if (isFrist) data.isLeftStop = true else data.isRightStop = true
    }
}

interface FlowViewItemListener {
    fun onTopClickItem(isFrist: Boolean)
}